home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Graphics / SMan / WarpOS / Source / Save.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-28  |  4.9 KB  |  183 lines

  1. #include    <clib/all_protos.h>
  2. #include    <exec/memory.h>
  3. #include    <iff/iff.h>
  4. #include    <iff/ilbm.h>
  5. #include    <iffp/packer.h>
  6.  
  7.  
  8. extern WORD WWidth,WHeight,WDepth;
  9. extern struct Screen *WBScreen;
  10. extern struct BitMap *TmpBitMap;
  11. extern struct BitMap *NWBitMap;
  12. extern struct RastPort VTmpRast;
  13. extern struct RastPort *CWRast;
  14. extern struct RastPort NWRast;
  15. extern LONG BLeft,BTop;
  16. extern int WriteFile(char *,ULONG);
  17. extern UBYTE *GetColors(UBYTE *,ULONG);
  18. extern int OpenWrite(UBYTE *);
  19. extern void CloseWrite(void);
  20. extern void ChangeNW(void);
  21. extern void DisLine(struct RastPort *,char *,int);
  22. extern struct NewWindow nw;
  23. extern struct TextFont *CWFont;
  24. extern struct Window *IntWindow;
  25. extern struct GfxBase *GfxBase;
  26.  
  27. void Save(void)
  28. {
  29. ULONG    CTSize,CTCount,HeaderLength,BodyLength,ActualBody,LineLength;
  30. LONG    PixCnt;
  31. ULONG    *HdrSize,*HdrTop,*HdrBottom;
  32. UBYTE    *MyColors;
  33. ChunkHeader        *Chunk3D;
  34. BitMapHeader    *MyHeader;
  35.  
  36. typedef        struct    {
  37.     ID        ckID;
  38.     }    sChunk;
  39.  
  40. SHORT PlnCnt;
  41. UBYTE *OffPtr;
  42. struct Window *SavingWnd;
  43. UBYTE TmpArray[1600];
  44. sChunk    *ChunkID;
  45. char    *HdrBuffer,*BodyBuffer,*BytPtr;
  46.  
  47. ULONG    *ModBuf;
  48. USHORT    LineCount;
  49.     HdrBuffer = AllocMem(1200,MEMF_PUBLIC|MEMF_CLEAR);
  50.     if (!HdrBuffer) {
  51.         printf("Couldn't allocate memory for the header\n");
  52.         return;
  53.         }
  54.     Chunk3D = (ChunkHeader *) HdrBuffer;
  55.     Chunk3D -> ckID = FORM;
  56.     Chunk3D++;
  57.     ChunkID = (sChunk *) Chunk3D;
  58.     ChunkID -> ckID = ID_ILBM;
  59.     ChunkID++;
  60.     ChunkID -> ckID = ID_BMHD;
  61.     ChunkID++;
  62.     HdrSize = (ULONG *)ChunkID;
  63.     *HdrSize++ = 20;
  64.     MyHeader = (BitMapHeader *)HdrSize;
  65.     MyHeader -> w = WWidth;
  66.     MyHeader -> h = WHeight;
  67.     MyHeader -> compression = 1;
  68.     CTCount = WDepth;
  69.     MyHeader -> nPlanes = CTCount;
  70.     MyHeader -> xAspect = 10;
  71.     MyHeader -> yAspect = 10;
  72.     MyHeader -> pageWidth = WWidth;
  73.     MyHeader -> pageHeight = WHeight;
  74.     MyHeader++;
  75.     Chunk3D = (ChunkHeader *)MyHeader;
  76.     Chunk3D -> ckID = ID_CMAP;
  77.     CTSize = 1;
  78.     while (CTCount)    {
  79.         CTSize <<= 1;
  80.         --CTCount;
  81.         }
  82.     Chunk3D -> ckSize = 3*CTSize;
  83.     Chunk3D++;
  84.     MyColors = (UBYTE *)Chunk3D;
  85.     CTCount = WDepth;
  86.     MyColors = GetColors(MyColors,CTSize);
  87.     Chunk3D = (ChunkHeader *)MyColors;
  88.     Chunk3D -> ckID = ID_CAMG;
  89.     Chunk3D -> ckSize = 4;
  90.     Chunk3D++;
  91.     ModBuf = (ULONG *)Chunk3D;
  92.     *ModBuf++ = WBScreen->ViewPort.Modes;
  93.     Chunk3D = (ChunkHeader *)ModBuf;
  94.     Chunk3D -> ckID = ID_BODY;
  95.     CTCount = WWidth;
  96.     CTCount >>= 3;
  97.     CTCount = CTCount*WHeight;
  98.     CTCount = CTCount*WDepth;
  99.     TmpBitMap = (struct BitMap *)AllocBitMap((ULONG)WWidth,1,WDepth,BMF_CLEAR|BMF_DISPLAYABLE,WBScreen->RastPort.BitMap);
  100.     if (!TmpBitMap) {
  101.         printf("Couldn't allocate the 1st BitMap!\n");
  102.         if (HdrBuffer) FreeMem(HdrBuffer,1200);
  103.         }
  104.     InitRastPort(&VTmpRast);
  105.     VTmpRast.Layer = NULL;
  106.     VTmpRast.BitMap = TmpBitMap;
  107.     NWBitMap = (struct BitMap *)AllocBitMap((ULONG)WWidth,WHeight,WDepth,BMF_CLEAR,0);
  108.     if (!NWBitMap) {
  109.         printf("Couldn't allocate the 2nd BitMap!\n");
  110.         FreeBitMap(TmpBitMap);
  111.         if (HdrBuffer) FreeMem(HdrBuffer,1200);
  112.         }
  113.     InitRastPort(&NWRast);
  114.     NWRast.BitMap = NWBitMap;
  115.     CTCount = NWRast.BitMap->BytesPerRow;
  116.     CTCount = CTCount*WHeight;
  117.     CTCount = CTCount*WDepth;
  118.     BodyLength = CTCount;
  119.     BodyBuffer = AllocMem(BodyLength,MEMF_CLEAR);
  120.     if (!BodyBuffer) {
  121.         printf("Couldn't allocate memory for the image!\n");
  122.         FreeBitMap(NWBitMap);
  123.         FreeBitMap(TmpBitMap);
  124.         if (HdrBuffer) FreeMem(HdrBuffer,1200);
  125.         }
  126.     ChangeNW();
  127.     SavingWnd = OpenWindow(&nw);
  128.     SetFont(SavingWnd->RPort,CWFont);
  129.     BytPtr = BodyBuffer;
  130.     PlnCnt = 0;
  131.     LineCount = 0;
  132.     while (LineCount < WHeight)    {
  133.         ReadPixelLine8(CWRast,BLeft,LineCount+BTop,WWidth,&TmpArray[0],&VTmpRast);
  134.         WritePixelLine8(&NWRast,0,LineCount,WWidth,&TmpArray[0],&VTmpRast);
  135.         DisLine(SavingWnd->RPort,"Prepping Line ",WHeight - LineCount);
  136.         LineCount++;
  137.         }
  138.     LineCount = 0;
  139.     ActualBody = 0;
  140.     LineLength = NWRast.BitMap->BytesPerRow;
  141.     while (LineCount < WHeight)    {
  142.         PlnCnt = 0;
  143.         while (PlnCnt < WDepth) {
  144.             OffPtr = (UBYTE *)NWRast.BitMap->Planes[PlnCnt];
  145.             OffPtr = OffPtr + LineCount*LineLength;
  146.             PixCnt = 0;
  147.             PixCnt = PackRow((BYTE **)OffPtr,(BYTE **)BytPtr,(LONG)LineLength);
  148.             if (!PixCnt) printf("0 returned, and Body Size Current is %lx\n",(ULONG)ActualBody);
  149.             ActualBody = ActualBody + PixCnt;
  150.             PlnCnt++;
  151.             }
  152.         DisLine(SavingWnd->RPort,"Saving Line ",WHeight - LineCount);
  153.         Delay(1);
  154.         ++LineCount;
  155.         }
  156. /*    printf("The body length is %x\n",ActualBody);*/
  157.     if ((ActualBody/2)*2 != ActualBody) {
  158.         Chunk3D -> ckSize = ActualBody;
  159.         ActualBody++;
  160.             }
  161.     else Chunk3D -> ckSize = ActualBody;
  162.     Chunk3D++;
  163.     HdrTop = (ULONG *)Chunk3D;
  164.     HdrBottom = (ULONG *)HdrBuffer;
  165.     HeaderLength = HdrTop - HdrBottom;
  166.     HeaderLength <<= 2;     /*Get # bytes */
  167.     Chunk3D = (ChunkHeader *)HdrBuffer;
  168.     CTCount = ActualBody + HeaderLength;
  169.     Chunk3D -> ckSize = CTCount - 8;
  170.     Delay(50);
  171.     OpenWrite((UBYTE *)"Mand.iff");
  172.     WriteFile(HdrBuffer,HeaderLength);
  173.     WriteFile(BodyBuffer,ActualBody);
  174.     CloseWrite();
  175.     Delay(50);
  176.     if (SavingWnd) CloseWindow(SavingWnd);
  177.     if (BodyBuffer) FreeMem(BodyBuffer,BodyLength);
  178.     if (HdrBuffer) FreeMem(HdrBuffer,1200);
  179.     FreeBitMap(NWBitMap);
  180.     FreeBitMap(TmpBitMap);
  181. }
  182.  
  183.